package com.duoec.magichour.controller.api;

import com.duoec.magichour.core.web.BaseApiController;
import com.duoec.magichour.dto.BaseResponse;
import com.duoec.magichour.dto.response.ProductSimpleInfoDto;
import com.duoec.magichour.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 商品接口
 */
@RestController
@RequestMapping("/api/product")
public class ProductApiController extends BaseApiController {
    @Autowired
    private ProductService productService;

    /**
     * 查找商品
     *
     * @param keyword  搜索关键词（商品名称）
     * @param pageNo   当前分页数，1为第一页
     * @param pageSize 每页最多获取记录数
     * @return
     */
    @RequestMapping("/search")
    public BaseResponse<List<ProductSimpleInfoDto>> search(
            @RequestParam String keyword,
            @RequestParam int pageNo,
            @RequestParam(required = false, defaultValue = "20") int pageSize
    ) {
        //获取当前用户的ID
        long userId = getUserInfo().getId();
        List<ProductSimpleInfoDto> productList = productService.searchProducts(userId, keyword, pageNo, pageSize);
        return BaseResponse.success(productList);
    }
}
